package com.snailvr.tv.provider;


import android.util.Log;

import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.Utils;
import com.snailvr.tv.Definition;
import com.snailvr.tv.event.LoadFailureEvent;
import com.snailvr.tv.network.RetrofitFactory;
import com.snailvr.tv.network.entity.ChannelInfoResponBean;
import com.snailvr.tv.network.service.ChannelInfoService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/2.
 */
public class ChanelInfoProvider implements IChanelInfoProvider {

    private ChannelInfoService mChannelInfoService;
    private ChannelInfoResponBean mChannelInfoResponBean;

    public ChanelInfoProvider() {
        mChannelInfoService = RetrofitFactory.getmChannelInfoService();
    }

    @Override
    public void getChanelInfo(int channel_id) {
        getChannelInfoFromServer(channel_id);

    }

    @Override
    public void getDefaultInfo(String tag) {
        getDefaultInfoFromServer(tag);
    }


    @Override
    public void switchChannel(int channel_id, String type) {
        Call<ChannelInfoResponBean> call = mChannelInfoService.switchChannel(Utils.sessionid,Utils.token,channel_id,type);

        call.enqueue(new Callback<ChannelInfoResponBean>() {
            @Override
            public void onResponse(Call<ChannelInfoResponBean> call, Response<ChannelInfoResponBean> response) {
                if (response.body() != null) {
                    mChannelInfoResponBean = response.body();
                    if(mChannelInfoResponBean.getCode() == 0) {
                        Log.i("App", "message:" + mChannelInfoResponBean.getMessage());
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, mChannelInfoResponBean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, new LoadFailureEvent(mChannelInfoResponBean.getCode(), mChannelInfoResponBean.getMessage()));

                    }
                }
            }

            @Override
            public void onFailure(Call<ChannelInfoResponBean> call, Throwable t) {
                Log.d("App", "onFailure!");
                EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, new LoadFailureEvent(0, "切换频道请求失败"));

            }
        });
    }

    private  void getDefaultInfoFromServer(String tag) {

        Call<ChannelInfoResponBean> call = mChannelInfoService.getDefaultInfo(Utils.sessionid,Utils.token,tag);

        call.enqueue(new Callback<ChannelInfoResponBean>() {
            @Override
            public void onResponse(Call<ChannelInfoResponBean> call, Response<ChannelInfoResponBean> response) {
                if (response.body() != null) {
                    mChannelInfoResponBean = response.body();
                    if(mChannelInfoResponBean.getCode() == 0) {
                        Log.i("App", "message:" + mChannelInfoResponBean.getMessage());
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, mChannelInfoResponBean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, new LoadFailureEvent(mChannelInfoResponBean.getCode(), mChannelInfoResponBean.getMessage()));

                    }
                }
            }

            @Override
            public void onFailure(Call<ChannelInfoResponBean> call, Throwable t) {
                Log.d("App", "onFailure!");
                EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, new LoadFailureEvent(0, "默认频道详情请求失败"));

            }
        });
    }

    private void getChannelInfoFromServer(int channel_id) {


        Call<ChannelInfoResponBean> call = mChannelInfoService.getChannelInfo(Utils.sessionid,Utils.token,channel_id);

        call.enqueue(new Callback<ChannelInfoResponBean>() {
            @Override
            public void onResponse(Call<ChannelInfoResponBean> call, Response<ChannelInfoResponBean> response) {
                if (response.body() != null) {
                    mChannelInfoResponBean = response.body();
                    if(mChannelInfoResponBean.getCode() == 0) {
                        Log.i("App", "message:" + mChannelInfoResponBean.getMessage());
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, mChannelInfoResponBean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, new LoadFailureEvent(mChannelInfoResponBean.getCode(), mChannelInfoResponBean.getMessage()));

                    }
                }
            }

            @Override
            public void onFailure(Call<ChannelInfoResponBean> call, Throwable t) {
                Log.d("App", "onFailure!");
                EventBus.getDefault().post(Definition.BusTag.CHANNLE_INFO_TAG, new LoadFailureEvent(0, "频道详情请求失败"));

            }
        });


    }
}
